package com.hdu.distributeLock.lock;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static java.util.UUID.randomUUID;


@Component
public class DistributeLockFactory implements InitializingBean {

    private static final Map<String, DistributedRedisLock> LOCK_CACHE = new ConcurrentHashMap<>();

    private String applicationId;

    @Override
    public void afterPropertiesSet() throws Exception {
        applicationId = randomUUID().toString().replaceAll("-", "");
    }


    public DistributedRedisLock getDistributedRedisLock(String lockName) {
         LOCK_CACHE.putIfAbsent(lockName, new DistributedRedisLock(lockName, applicationId));
         return LOCK_CACHE.get(lockName);
    }
}
